package com.ygqh.baby.controller.admin;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.service.AdminUserService;
import com.ygqh.baby.utils.JsonUtils;

@Controller
@RequestMapping("/admin/sysuser")
public class AdminUserController extends BaseController<Object, java.lang.Long>{
    

	@Autowired
	private AdminUserService adminUserService;
	
	@RequestMapping("searchUser")
	@ResponseBody
	public Map<String, Object> searchUser(String q, QueryInfo queryInfo){
		ResultSet<AdminUser> resultSet = adminUserService.search(queryInfo,q);
	return resultSet.toDatatables(null);

	}
	
	@RequestMapping("getUser")
	@ResponseBody
	public AdminUser getUser(Long userId){
		return  adminUserService.findById(userId);	
	}
	
	@RequestMapping("saveUser")
	@ResponseBody
	public Message saveUser(String user,String roles){
		AdminUser adminUser=JsonUtils.toObject(user,AdminUser.class);	
		adminUser.setCreateTime(new Date());
		adminUser.setSupplierId(getSession().getSupplierId());
		adminUserService.saveOrUpdate(adminUser);
		
		return SUCCESS_MESSAGE;
	}
	@RequestMapping("delUser")
	@ResponseBody
	public Message delUser(Long id){
		
		adminUserService.deleteSoft(id,getSession().getTrueName());
		return SUCCESS_MESSAGE;
	}
	
	@RequestMapping("findByUserName")
	@ResponseBody
	public boolean findByUserName(Long id,String userName){
		AdminUser user=adminUserService.findByUserName(userName);
		if(user!=null){
			if(id==null)
				return true;
			else if(user.getId()!=id)
				return true;
		}
		
		return false;
		
		
	}
	
	
}
